package com.yj.web.controllers.admin;

import com.yj.cache.service.YjCacheService;
import com.yj.passport.service.PassportService;
import com.yj.web.annotation.LoginRequired;
import com.yj.web.dao.po.admin.Employee;
import com.yj.web.dao.po.admin.Introducer;
import com.yj.web.dao.po.admin.UserProjectByIntroducer;
import com.yj.web.model.BaseWebResult;
import com.yj.web.service.IntroducerService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanlinyang on 2017/7/25.
 */
@Controller
@RequestMapping("/admin")
public class IntroducerController extends BaseController {
    private static final Log logger = LogFactory.getLog(IntroducerController.class);

    @Autowired
    private IntroducerService introducerService;
    @Autowired
    private YjCacheService passportCache;
    @Autowired
    private PassportService passportService;


    @LoginRequired
    @RequestMapping(value="/listIntroducer", method = RequestMethod.POST)
    public @ResponseBody
    BaseWebResult listIntroducer(HttpServletRequest request, @RequestParam(value = "page", defaultValue= "1") int page, @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        List<Introducer> introducers = null;
        long employeeId = getHostId(request);
        try {
            introducers = introducerService.listIntroducer(employeeId, page, size);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        result.setData(introducers);
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/countIntroducer", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countIntroducer(HttpServletRequest request) {
        BaseWebResult result = new BaseWebResult();
        long employeeId = getHostId(request);
        Integer num = introducerService.countIntroducer(employeeId);
        result.setData(num);
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/deleteIntroducerById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult deleteIntroducerById(@RequestParam("id") long id) {
        BaseWebResult result = new BaseWebResult();
        int b = introducerService.deleteIntroducerById(id);
        result.setData(b);
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/insertIntroducer", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult insertIntroducer(@ModelAttribute("introducer") Introducer introducer) {
        BaseWebResult result = new BaseWebResult();

        try {
            introducerService.insertIntroducer(introducer);
            result.setMsg("增加成功");
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/introducerRelatedEmployee", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult introducerRelatedEmployee(HttpServletRequest request) {
        BaseWebResult result = new BaseWebResult();
        long employeeId = getHostId(request);
        try {
            List<Employee> employees = introducerService.introducerRelatedEmployee(employeeId);
            result.setData(employees);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/updateIntroducer", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult updateIntroducer(@ModelAttribute("introducer") Introducer introducer) {
        BaseWebResult result = new BaseWebResult();
        try {
            introducerService.updateIntroducer(introducer);
            result.setMsg("更新成功");
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/updateIntroducerStatus", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult updateIntroducerStatus(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        BaseWebResult result = new BaseWebResult();
        try {
            boolean flag = introducerService.updateIntroducerStatus(id, status);
            if (false == flag) {
                result.setCd(1);
                result.setMsg("系统错误");
            } else {
                result.setMsg("Introducer状态更新成功");
            }
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/listUserProjectByIntroducer", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult listUserProjectByIntroducer(HttpServletRequest request, @RequestParam("nick_name") String nick_name,
                                                                   @RequestParam("introducer_name") String introducer_name,
                                                                   @RequestParam("phone") String phone,
                                                                   @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        long employeeId = getHostId(request);
        List<UserProjectByIntroducer> userProjectByIntroducers = new ArrayList<>();
        try {
            userProjectByIntroducers = introducerService.listUserProjectByIntroducer(nick_name, introducer_name, phone, employeeId, page, size);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        result.setData(userProjectByIntroducers);
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/countUserProjectByIntroducer", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countUserProjectByIntroducer(HttpServletRequest request, @RequestParam("nick_name") String nick_name,
                                                                   @RequestParam("introducer_name") String introducer_name,
                                                                   @RequestParam("phone") String phone) {
        BaseWebResult result = new BaseWebResult();
        long employeeId = getHostId(request);
        try {
            int num = introducerService.countUserProjectByIntroducer(nick_name, introducer_name, phone, employeeId);
            result.setData(num);
            result.setMsg("更新成功");
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/getIntroducerById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult getIntroducerById(@RequestParam("id") long id) {
        BaseWebResult result = new BaseWebResult();
        try {
            Introducer introducer = introducerService.getIntroducerById(id);
            result.setData(introducer);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/updateQrcode", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult updateQrcode(@RequestParam("id") int id, @RequestParam("QRcode") String QRcode) {
        BaseWebResult result = new BaseWebResult();
        try {
            introducerService.updateQrcode(id, QRcode);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @RequestMapping(value = "/getIntroPhoneByOpenId", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult getIntroPhoneByOpenId(@RequestParam("openId") String openId) {
        BaseWebResult result = new BaseWebResult();
        try {
            String introPhone = introducerService.getIntroPhoneByOpenId(openId);
            result.setData(introPhone);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("获取渠道手机号错误！");
        }
        return result;
    }

}
